package dao.mysql;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

import model.CompensationLeaveDTO;
import model.LeaveDTO;
import dao.CompensationLeaveDAO;
import dao.CompensationLeaveDAO1;
import exception.DAOSystemException;

public class CompensationLeaveDAOImpl1 implements CompensationLeaveDAO1
{
	MySqlImpl mysqlImpl = new MySqlImpl();
	public CompensationLeaveDTO find(String id) throws DAOSystemException {
		// TODO Auto-generated method stub
		
        System.out.println(""+id);
		String selectSql = "SELECT * FROM `leaveapplicationdb`.`compensationleave` WHERE id='"+id+"';";
		CompensationLeaveDTO oneCompensationLeave = new CompensationLeaveDTO();
		try{
			ResultSet rs = mysqlImpl.get(selectSql); 
			
			
			if (rs.next()) {
				oneCompensationLeave.setId(rs.getString("id"));
				oneCompensationLeave.setTotalOT(rs.getDouble("totalOT"));
		        oneCompensationLeave.setConsumeOT(rs.getDouble("consumeOT"));
		        oneCompensationLeave.setBalanceOT(rs.getDouble("balanceOT"));
			}
			
		} catch (Exception e) {
			String error = "Error selecting compensationleave. Nested Exception is: " + e;
			Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
			throw new DAOSystemException(error);
		} finally {
			try {
				mysqlImpl.close();
			} catch (Exception e) {
			}
		}
		
		return oneCompensationLeave;
	}
	
	public void updateForApproval(String userid,double total)
	throws DAOSystemException {
		// TODO Auto-generated method stub
		String updateSql = "UPDATE `leaveapplicationdb`.`compensationleave` "+
        "SET consumeOT= '"+total+"' "+
        "Where id='"+userid+"';";
		try {
		
		Logger.getLogger(getClass().getName()).log(Level.INFO,
			"Executing update: " + updateSql);
		int res = mysqlImpl.execute(updateSql);
		
		} catch (Exception e) {
		String error = "Error selecting compensationleave. Nested Exception is: "
			+ e;
		Logger.getLogger(getClass().getName()).log(Level.SEVERE, error);
		throw new DAOSystemException(error);
		} finally {
		try {
		mysqlImpl.close();
		} catch (Exception e) {
		}
		}
	}
}

